//
//  RegisterViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/8.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()
{
    int timeCount;
}
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *pswTextField;
//@property (weak, nonatomic) IBOutlet UITextField *rePswTextField;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@property (weak, nonatomic) IBOutlet UILabel *codeTimeLable;

@property (strong, nonatomic) NSTimer *timer;
@end

@implementation RegisterViewController
- (void)backAction:(UIButton *)btn {
    [self dismissViewControllerAnimated:YES completion:^{
        [_timer invalidate];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initNav:@"快速注册" doModel:YES];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHident:) name:UIKeyboardWillHideNotification object:nil];
    
    _scrollView.contentSize = CGSizeMake(ScreenWidth, 230);
    
    _codeBtn.layer.cornerRadius = 12.0;
    _codeBtn.layer.borderColor = SetColor(241, 174, 7, 1).CGColor;
    _codeBtn.layer.borderWidth = 1;
    
    timeCount = 60;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 隐藏键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (IBAction)tapHidentKB:(UITapGestureRecognizer *)sender {
    [self.view endEditing:YES];
}
#pragma mark - 通知事件
- (void)keyboardWillShow:(NSNotification *)notifi {
    NSDictionary* info = [notifi userInfo];
    //kbSize即為鍵盤尺寸 (有width, height)
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
    _scrollView.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-kbSize.height);
}
- (void)keyboardWillHident:(NSNotification *)notifi {
    _scrollView.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight-64);
}
#pragma mark -
- (IBAction)getCodeAction:(id)sender {
    [self.view endEditing:YES];
    //获取验证码
    if (_phoneNumTextField.text.length == 0) {
        [[MessShowView share] showWithMess:@"请输入手机号码"];
        return;
    }
    if (![Utils isMobileNum:_phoneNumTextField.text]) {
        [[MessShowView share] showWithMess:@"输入的手机号码有误"];
        return;
    }
    NSString *url = [NSString stringWithFormat:GetCode,_phoneNumTextField.text,[Utils md5SignStr]];
    [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSString *r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(theTimerValue) userInfo:nil repeats:YES];
            [_timer fire];
        }
        [[MessShowView share] showWithMess:dic[@"m"]];
    }];
}
- (IBAction)registerAction:(id)sender {
    [self.view endEditing:YES];
    //注册
    if (_codeTextField.text.length == 0) {
        [[MessShowView share] showWithMess:@"请输入验证码"];
        return;
    }
    if (_pswTextField.text.length == 0) {
        [[MessShowView share] showWithMess:@"请输入密码"];
        return;
    }
//    if (_rePswTextField.text.length == 0) {
//        [[MessShowView share] showWithMess:@"请输入确认密码"];
//        return;
//    }
    
    NSString *url = [NSString stringWithFormat:Register,_phoneNumTextField.text,_pswTextField.text,_pswTextField.text,_codeTextField.text,[Utils md5SignStr]];
    [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSString *r = dic[@"r"];
        [_timer invalidate];
        if ([r isEqualToString:@"T"]) {
            
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }else {
            _codeBtn.hidden = NO;
            _codeTimeLable.hidden = YES;
            timeCount = 60;
        }
        [[MessShowView share] showWithMess:dic[@"m"]];
    }];
}
#pragma mark - 倒计时
- (void)theTimerValue {
    timeCount -=1;
    if (timeCount == 0) {
        _codeBtn.hidden = NO;
        _codeTimeLable.hidden = YES;
        [_timer invalidate];
        timeCount = 60;
    }else {
        _codeBtn.hidden = YES;
        _codeTimeLable.hidden = NO;
        _codeTimeLable.text = [NSString stringWithFormat:@"剩余%d秒",timeCount];
    }
    
}

@end
